package com.six.controller;

import com.six.domain.dto.AddTableDto;
import com.six.domain.entity.Table;
import com.six.domain.entity.User;
import com.six.mapper.UserMapper;
import com.six.service.TableService;
import com.six.service.UserService;
import com.six.util.Result;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.six.enums.TableStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;

import java.nio.file.AccessDeniedException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/manager")
@RequiredArgsConstructor
public class ManagerController {

    private final TableService tableService;
    private final UserService userService;
    private final UserMapper userMapper;

    @PostMapping("/tables")
    public Result<Void> addTable(@RequestBody AddTableDto dto) {
        System.err.println("=== addTable 入参 === storeId=" + dto.getStoreId() + ", tableNumber=" + dto.getTableNumber());
        tableService.addTable(dto);
        return Result.ok();
    }

    @GetMapping("/tables")
    public ResponseEntity<List<Table>> getAllTables() {
        List<Table> tables = tableService.getAllTablesByStore(1L); // 假设门店ID为1
        return ResponseEntity.ok().body(tables);
    }
    @PatchMapping("/tables/{id}/status")
    public ResponseEntity<Void> updateTableStatus(
            @PathVariable Long id,
            @RequestBody Map<String, String> body
    ) {
        String status = body.get("status");
        tableService.updateTableStatus(id, status);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/waiters")
    @PreAuthorize("hasRole('MANAGER')")
    public Result<List<User>> listWaiters(@RequestParam Long storeId,
                                          Authentication authentication) throws AccessDeniedException {
        return Result.ok(userService.listWaitersByStore(storeId, authentication));
    }
    @DeleteMapping("/waiter/{id}")
    @PreAuthorize("hasRole('MANAGER')")
    public Result<Void> deleteWaiter(@PathVariable Long id) {
        userMapper.deleteById(id);// 或逻辑删除
        return Result.ok();
    }
}